package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    @Override
    public void work(BookList booklist) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("删除图书...");
        System.out.println("请输入要删除的图书:");
        String name = scanner.nextLine();
        int i = 0;
        int currentSize = booklist.getUsedSize();
        for (i = 0; i < currentSize; i++) {
            if(booklist.findBook(i).getName().equals(name)) {
                int index = i;
                for (int j = index; j < currentSize-1; j++) {
                    Book book = booklist.findBook(j+1);
                    booklist.setBook(j,book);
                }
                booklist.setUsedSize(currentSize-1);
                System.out.println("删除成功！");
                System.out.println("--------------------------------------");                break;
            }
        }
        if(i >= currentSize) {
            System.out.println("该图书尚未添加...");
            System.out.println("--------------------------------------");            return;
        }
    }
}
